package com.xinnet.mms.customerConsumeManage.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.allot.dao.CustomMixDao;
import com.xinnet.mms.allot.service.CustomAllotService;
import com.xinnet.mms.config.dao.FlowTurnConfigDao;
import com.xinnet.mms.customerConsumeManage.dao.CustomerConsumeManageDao;
import com.xinnet.mms.customerConsumeManage.service.CustomerConsumeManageService;
import com.xinnet.mms.customercare.dao.CustomerCareDao;
import com.xinnet.mms.dept.dao.DeptDao;
import com.xinnet.mms.dept.service.DeptService;
import com.xinnet.mms.deptarea.dao.DeptAreaDao;
import com.xinnet.mms.empcustomrelation.dao.EmpCustomRelationDao;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.CustomerConsume;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.enums.CustomerTypeEnmu;
import com.xinnet.mms.login.dao.EmpDao;
import com.xinnet.mms.member.service.MemberService;
import com.xinnet.mms.order.dao.OrderDao;

@Service("customerConsumeManageService")
public class CustomerConsumeManageServiceImpl implements CustomerConsumeManageService,Serializable{

	private static final long serialVersionUID = 3776313645521761556L;

	@Autowired
	private CustomerConsumeManageDao customerConsumeManageDao;

	@Autowired
	private CustomerCareDao customerCareDao;

	@Autowired
	private transient FlowTurnConfigDao flowTurnConfigDao;

	@Autowired
	private transient EmpDao empDao;

	@Autowired
	private CustomAllotService customAllotService;
	
	@Autowired
	private transient OrderDao orderDao;

	@Autowired
	private transient DeptDao deptDao;
	
	@Autowired
	private transient DeptAreaDao deptAreaDao;
	
	@Autowired
	private EmpCustomRelationDao empCustomRelationDao;
	
	@Autowired
	private CustomMixDao  customMixDao;
	/** 会员中心 */
	@Autowired
	private MemberService memberService;
	
	private static Log log = LogFactory.getLog(CustomerConsumeManageServiceImpl.class);
	
	@Autowired
	private DeptService deptService;
	
	
	@Override
	public PageView<CustomerConsume> queryCustomerConsume(PageView<CustomerConsume> pageView,
			CustomerConsume customerConsume) throws Exception {
		int rowCount = customerConsumeManageDao.count(customerConsume);
		List<CustomerConsume> customerConsumeList = customerConsumeManageDao.queryCustomerConsume(pageView, customerConsume);
		
		for(CustomerConsume c:customerConsumeList) {
			Customer member = memberService.getMemberByCusId(c.getCusId());
			if(member!=null){
				c.setCname(member.getOrganizeNameCN());
				if(c.getCustomType()!=null&&!"".equals(c.getCustomType())){
					c.setCustomType(CustomerTypeEnmu.getCustomerTypeEnmuByValue(Integer.valueOf(c.getCustomType())).getLabel());
				}
			}
		}
		Integer pageSize = pageView.getPageSize();
		Integer pageCount = (rowCount + pageSize - 1) / pageSize;
		
		if(customerConsumeList!=null &&customerConsumeList.size()>0){
			customerConsumeList.get(0).setPageNow(pageView.getPageNow());
			customerConsumeList.get(0).setRowCount(rowCount);
			customerConsumeList.get(0).setPageSize(pageSize);
			customerConsumeList.get(0).setPageCount(pageCount);
		}
		
		pageView.setQueryResult(rowCount, customerConsumeList);
		pageView.setPageCount(pageCount);
		
		return pageView;
	}
	
	@Override
	public List<CustomerConsume> countCustomerConsume(
			CustomerConsume customerConsume) {
		List<CustomerConsume> customerConsumeList = customerConsumeManageDao.queryCustomerConsumeForCount(customerConsume);
		return customerConsumeList;
	}

	@Override
	public void addCustomerConsume(CustomerConsume customerConsume) {
		CustomerConsume customerConsumeForQuery = new CustomerConsume();
		customerConsumeForQuery.setProduct(customerConsume.getProduct());
		customerConsumeForQuery.setCusId(customerConsume.getCusId());
		Integer count = customerConsumeManageDao.count(customerConsumeForQuery);
		if(count!=null && count==0){
			this.customerConsumeManageDao.addCustomerConsume(customerConsume);
		} else {
			throw new RuntimeException();
		}
		
	}

	@Override
	public void updateCustomerConsume(CustomerConsume customerConsume) {

		this.customerConsumeManageDao.updateCustomerConsume(customerConsume);
	}
	
	@Override
	public void delCustomerConsume(CustomerConsume customerConsume) {

		this.customerConsumeManageDao.delCustomerConsume(customerConsume);
	}
	
	@Override
	public void updateCustomerConsumeEmp(Customer customer,Emp emp) {

		this.customerConsumeManageDao.updateCustomerConsumeEmp(customer,emp);
	}
	
	@Override
	public void updateCustomerConsumeCusId(Customer reported,Customer customer) {

		this.customerConsumeManageDao.updateCustomerConsumeCusId(reported,customer);
	}
}
